
package com.ostrich.manpower.service.impl;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.service.BaseService;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrSubsidyinfo;
import com.ostrich.manpower.common.DateUtil;
import com.ostrich.manpower.common.ImportUtil;
import com.ostrich.manpower.service.TostHrSubsidyinfoDao;
/**
 * @author wangyinan
 * @brief    模块功能简单描述
 * @details  模块功能详细描述
 */
@Service("tostHrSubsidyinfoService")
public class TostHrSubsidyinfoService<T> extends BaseService<T> {
	private final static Logger log= Logger.getLogger(TostHrSubsidyinfoService.class);

	@Autowired
    private TostHrSubsidyinfoDao<T> dao;
	
	@Autowired
	TostHrBaseinfoService<T>  tosthrbaseinfoservice;
	
	public TostHrSubsidyinfoDao<T> getDao() {
		return dao;
	}
	
	public List<T> querySubsidyinfoByHrId(int hrId){
		return dao.querySubsidyinfoByHrId(hrId);
	}
	/****
	 * 根据sheet插入数据库
	 * @param sheet
	 * @return
	 */
	public String setSubsidyinfoBySheet(HSSFSheet sheet) throws Exception{
		//List<TostHrSubsidyinfo> infos =new ArrayList<TostHrSubsidyinfo>();
	    HSSFRow row = null;
	    int colnum = 15;
	    String msg ="";
		 //遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数  除去标题跟表头
	     for(int j = 2; j < sheet.getPhysicalNumberOfRows(); j++) {
	         row = sheet.getRow(j);  
	         TostHrSubsidyinfo tmeal = new TostHrSubsidyinfo();  
	         String strHrCode = ImportUtil.getCellValue(row.getCell(0));
	         //查询第二列中的员工编号是否存在，如果存在才进行人员补贴的添加，否则不增加补贴信息
	         TostHrBaseinfo bainfo= (TostHrBaseinfo) tosthrbaseinfoservice.getBaseinfoByHrCode(strHrCode);
	         
	         if(strHrCode!=null){
					if(bainfo==null||bainfo.getId() == null){
						msg +="第"+(j+1)+"行记录异常：工号："+strHrCode+"，姓名为："+ImportUtil.getCellValue(row.getCell(1))+"的考勤没有正常导入（工号不存在）。";
						continue;
					}
				}
	         
	         if(strHrCode!=null && strHrCode.length()>0 && (ImportUtil.getCellValue(row.getCell(1))).equalsIgnoreCase(bainfo.getHrName()) ){
	        	 Float knowadmin=new Float(0);
	        	 Float expert = new Float(0);
	        	 Float isosubsidy = new Float(0);
		         for(int i=0;i<colnum;i++ ){
		        	 String str = ImportUtil.getCellValue(row.getCell(i));
		        	if(i==0){
		        		tmeal.setHrId(bainfo.getId());
		        	}else if(i==1){
		        	     tmeal.setHsbName(bainfo.getHrName());
		        	 }else if(i==2){
		        		 tmeal.setHsbDepart(bainfo.getHrDepart());
		        	 }else if(i==3){
		        		 Float flstr = (str!=null && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.valueOf(str):new Float(0);
		        		 tmeal.setHsbEducation(flstr);
		        	 }else if(i==4){
		        		 Float flstr = (str!=null && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.valueOf(str):new Float(0);
		        		 tmeal.setHsbComputer(flstr);
		        	 }else if(i==5){
		        		 Float flstr = (str!=null && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.valueOf(str):new Float(0);
		        		 tmeal.setHsbKnowadmin(flstr);
		        		 knowadmin =(flstr!=null)?flstr:new Float(0);
		        	 }else if(i==6){
		        		 Float flstr = (str!=null && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.valueOf(str):new Float(0);
		        		 tmeal.setHsbExpert(flstr);
		        		 expert=(flstr!=null)?flstr:new Float(0);
		        	 }else if(i==7){
		        		 Float flstr = (str!=null && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.valueOf(str):new Float(0);
		        		 tmeal.setHsbTraffic(flstr);
		        		 isosubsidy=(flstr!=null)?flstr:new Float(0);
		        	 }else if(i==8){
		        		//Float flstr = (str!=null && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.valueOf(str):new Float(0);
		        		 Float flstr = (str!=null && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.valueOf(str):new Float(0);
		        		  //tmeal.setHsbSkillsubs(knowadmin+expert+isosubsidy);
		        		 tmeal.setHsbCommunication(flstr);
			        }else if(i==9){
			        		 Float flstr = (str!=null && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.valueOf(str):new Float(0);
			        		 tmeal.setHsbRemote(flstr);
		        	}else if(i==10){
			        		 Float flstr = (str!=null && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.valueOf(str):new Float(0);
			        		 tmeal.setHsbOthersubs(flstr);
			        }
		         }
		         
		         TostHrSubsidyinfo subbs=(TostHrSubsidyinfo) queryByHrid(bainfo.getId());
		         //添加数据到数据库
		         if(tmeal!=null && tmeal.getHsbName()!=null && tmeal.getHsbName().trim().length()>0){
		        	 String hs = DateUtil.getYearMonthByNow();
	        		 tmeal.setHsbMonth(hs);
	        		 if(subbs!=null){
		        		 tmeal.setId(subbs.getId());
		        		 dao.update((T)tmeal); 
		        	 }else{
				         dao.add((T) tmeal);
				         //infos.add(tmeal);
		        	 }
		         }
	         }else{
				 msg +="第"+(j+1)+"行记录异常：工号："+strHrCode+"，姓名为："+ImportUtil.getCellValue(row.getCell(1))+"的考勤没有正常导入（工号跟姓名不匹配）。";
		       	 	continue;
				 }
	     }
	     return msg;
	}

	
	public String setSubsidyinfoCellVal(String filepath) throws Exception {
		InputStream fis = new FileInputStream(filepath);
		HSSFSheet sh = ImportUtil.getSheet(fis);
		String msg = setSubsidyinfoBySheet(sh);// 写数据库的方法
		return msg;
	}
	
	public T queryByHrid(int hrId) {
		
		return getDao().queryByHrid(hrId);
	}
	
	//根据名字查合计
	public  List<T> queryAddByTostHrSubsidyinfo(TostHrSubsidyinfo tosthrsubsidyinfo,String startM,String endM){
		return getDao().queryAddByTostHrSubsidyinfo(tosthrsubsidyinfo,startM,endM);
	}
}
